package com.chhuang.system.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chhuang.core.bean.QueryBean;
import com.chhuang.core.service.impl.BaseServiceImpl;
import com.chhuang.system.bean.MenuWithRoleIdQueryBean;
import com.chhuang.system.mapper.ChRoleMenuMapMapper;
import com.chhuang.system.po.ChMenu;
import com.chhuang.system.po.ChRoleMenuMap;
import com.chhuang.system.po.ChRoleMenuMapExample;
import com.chhuang.system.service.IChRoleMenuMapService;

/**
* ChRoleMenuMap ServiceImpl
* @author ChHuang
* @date Fri Mar 03 13:20:59 CST 2017
*/
@Service
public class ChRoleMenuMapServiceImpl extends BaseServiceImpl<ChRoleMenuMapMapper, ChRoleMenuMap> implements IChRoleMenuMapService {
	private static final Log log = LogFactory.getLog(ChUserRoleMapServiceImpl.class);
	@Autowired
	private ChRoleMenuMapMapper chRoleMenuMapMapper;

	@Override
	public QueryBean<ChRoleMenuMap> seniorQuery(QueryBean<ChRoleMenuMap> queryBean) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public List<ChRoleMenuMap> findByRoleId(String roleId) throws Exception {
		ChRoleMenuMapExample example = new ChRoleMenuMapExample();
		example.setOrderByClause("menu_order asc");
		example.createCriteria().andRoleIdEqualTo(roleId);
		return chRoleMenuMapMapper.selectByExample(example);
	}

	@Override
	public int deleteByMenuId(String menuId) throws Exception {
		ChRoleMenuMapExample example = new ChRoleMenuMapExample();
		example.createCriteria().andMenuIdEqualTo(menuId);
		return chRoleMenuMapMapper.deleteByExample(example);
	}

	@Override
	public int deleteByMenuIdArray(String[] menuIds) throws Exception {
		ChRoleMenuMapExample example = new ChRoleMenuMapExample();
		example.createCriteria().andMenuIdIn(Arrays.asList(menuIds));
		return chRoleMenuMapMapper.deleteByExample(example);
	}

	@Override
	public int deleteByRoleIdArray(String[] roleIds) throws Exception {
		ChRoleMenuMapExample example = new ChRoleMenuMapExample();
		example.createCriteria().andRoleIdIn(Arrays.asList(roleIds));
		return chRoleMenuMapMapper.deleteByExample(example);
	}
	
	@Override
	public int deleteByRoleIdMenuId(String roleId, String menuId) throws Exception {
		ChRoleMenuMapExample example = new ChRoleMenuMapExample();
		example.createCriteria().andRoleIdEqualTo(roleId)
			.andMenuIdEqualTo(menuId);
		return chRoleMenuMapMapper.deleteByExample(example);
	}

	@Override
	public List<ChRoleMenuMap> findByMenuId(String menuId) throws Exception {
		ChRoleMenuMapExample example = new ChRoleMenuMapExample();
		example.setOrderByClause("menu_order asc");
		example.createCriteria().andMenuIdEqualTo(menuId);
		return chRoleMenuMapMapper.selectByExample(example);
	}

	@Override
	public QueryBean<ChMenu> findMenusByRoleIdAndQueryConditions(MenuWithRoleIdQueryBean queryBean) throws Exception {
		queryBean.setItemCount(chRoleMenuMapMapper.countMenusByRoleId(queryBean));//总记录数
		if(queryBean.getItemCount()>0){//计算总页数
			queryBean.setPageCount(queryBean.getItemCount()/queryBean.getPageSize()+
					(int)(queryBean.getItemCount()%queryBean.getPageSize()>0?1:0));
			if(queryBean.getPageCount()>=queryBean.getPageNum()){
				//分页
				queryBean.setOffset((queryBean.getPageNum()-1)*queryBean.getPageSize());
				queryBean.setResult(chRoleMenuMapMapper.selectMenusByRoleId(queryBean));
			}
		}
		return queryBean;
	}

}